package com.cskaoyan.day2;

import java.util.Scanner;

public class PrimeNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数n：");
        int n = sc.nextInt();
        System.out.println("n以内的素数：");
        int sum = 0;
        for (int i = 2; i <= n; i++) {
            if (isPrime(i)) {
                System.out.print(i + "\t");
                sum++;
            }
        }
        System.out.println("\n" + n + "以内的素数总计" + sum + "个");
    }

    public static boolean isPrime(int n) {
        int i;
        for (i = 2; i <= Math.sqrt(n); i++) {
            if (n % i == 0) {
                return false;
            }
        }
        return true;    // 反之则返回1
    }
}
